//
//  CommentTableViewCell.m
//  微blog
//
//  Created by Jorn.Wu on 15/8/29.
//  Copyright (c) 2015年 Jorn.Wu(jorn_wza@sina.com). All rights reserved.
//

#import "CommentTableViewCell.h"
#import "UIImageView+WebCache.h"
#import "WeiboCommentModel.h"
#import "UserModel.h"
#import "NSDateUtils.h"
#import "WXLabel.h"
#import "ThemeManager.h"

@interface CommentTableViewCell ()<WXLabelDelegate>

@property (weak, nonatomic) IBOutlet UIImageView *iconImageView;
@property (weak, nonatomic) IBOutlet UILabel *nameLb;
@property (weak, nonatomic) IBOutlet UILabel *dateLb;
@property (weak, nonatomic) IBOutlet UILabel *sourceLb;
@property (nonatomic, strong) WXLabel *mTextLabel;

@end
@implementation CommentTableViewCell

- (instancetype)initWithCoder:(NSCoder *)aDecoder{
    
    self = [super initWithCoder:aDecoder];
    if (self) {
        _mTextLabel = [[WXLabel alloc] initWithFrame:CGRectZero];
        _mTextLabel.font = [UIFont systemFontOfSize:14.0f];
        _mTextLabel.linespace = 5;
        _mTextLabel.wxLabelDelegate = self;
        [self.contentView addSubview:_mTextLabel];
        
        self.backgroundColor = [UIColor clearColor];
        
    }
    return self;
    
}

- (void)awakeFromNib{
    
    _mTextLabel = [[WXLabel alloc] initWithFrame:CGRectZero];
    _mTextLabel.font = [UIFont systemFontOfSize:14.0f];
    _mTextLabel.linespace = 5;
    _mTextLabel.wxLabelDelegate = self;
    [self.contentView addSubview:_mTextLabel];
    
    self.backgroundColor = [UIColor clearColor];

    
}

- (void)setCommentModel:(WeiboCommentModel *)commentModel{
    
    if (_commentModel != commentModel) {
        _commentModel = commentModel;
        
        UserModel *user = _commentModel.userModel;
        [self.iconImageView sd_setImageWithURL:[NSURL URLWithString:user.profile_image_url ]];
        
        self.nameLb.text = user.screen_name;        //昵称
        
        self.mTextLabel.text = _commentModel.text;      //评论文字
#warning -------源评论暂未定
        self.dateLb.text = [NSString stringWithFormat:@"时间：%@",[NSDateUtils weiboDateString:_commentModel.created_at]];      //日期
        
        self.sourceLb.text = _commentModel.source;      //来源

        [self setNeedsLayout];
        
    }
}

- (void)layoutSubviews{
    
    [super layoutSubviews];
    
    /**
     * 获得评论文字的高度（用于计算cell的高度）
     */
    CGFloat height = [WXLabel getTextHeight:14      //字体大小
                                      width:(kScreenWidth - 60 - 10)    //60 起始坐标，10 尾部间距
                                       text:self.commentModel.text
                                  linespace:5];       //行间距
    
    self.mTextLabel.frame = CGRectMake(70, CGRectGetHeight(self.iconImageView.frame) + 5, (kScreenWidth - 60 - 10), height);
    
}

+ (CGFloat)getCellHeightWithModel:(WeiboCommentModel *)model{
    
    /**
     * 获得评论文字的高度（用于计算cell的高度）
     */
    CGFloat height = [WXLabel getTextHeight:14
                                      width:(kScreenWidth - 60 - 10)
                                       text:model.text
                                  linespace:5];
    
    return height + 80;     //80是固定控件的空间
}


#pragma mark - WXLabelDelegate


- (NSString *)contentsOfRegexStringWithWXLabel:(WXLabel *)wxLabel
{
    //需要添加链接字符串的正则表达式：@用户、http://、#话题#
    NSString *regex1 = @"@\\w+";
    NSString *regex2 = @"http(s)?://([A-Za-z0-9._-]+(/)?)*";
    NSString *regex3 = @"#\\w+#";
    NSString *regex = [NSString stringWithFormat:@"(%@)|(%@)|(%@)",regex1,regex2,regex3];
    return regex;
}

//手指离开当前超链接文本响应的协议方法
- (void)toucheEndWXLabel:(WXLabel *)wxLabel withContext:(NSString *)context{
    kSinaWeiboLog(@"%@",context);
}

//设置当前链接文本的颜色
- (UIColor *)linkColorWithWXLabel:(WXLabel *)wxLabel{
    return [[ThemeManager defualtManager] getThemeColor:@"Link_color"];
}
//设置当前文本点击时的颜色
- (UIColor *)passColorWithWXLabel:(WXLabel *)wxLabel{
    return [UIColor grayColor];
}


















- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
